<template>
  <div class="box">
    <div class="search">
      <span>商品搜索：</span>
      <el-input v-model="input" placeholder="请输入"></el-input>
      <el-button type="primary" icon="el-icon-search" @click="searchSku">查询</el-button>
    </div>
    <div class="table">
      <el-button class="but" type="primary" icon="el-icon-circle-plus-outline" @click="handleAdd()">新建</el-button>
      <el-dialog class="newSkuDialog" title="收货地址" :visible.sync="dialogFormVisible1">
               <el-form :model="form">
                <el-form-item label="商品名称" :label-width="formLabelWidth">
                  <el-input v-model="form.name"></el-input>
                </el-form-item>
                <el-form-item label="品牌" :label-width="formLabelWidth">
                  <el-input v-model="form.name"></el-input>
                </el-form-item>
                <el-form-item label="商品类型" :label-width="formLabelWidth">
                  <el-input v-model="form.name"></el-input>
                </el-form-item>
                <el-form-item label="商品价格" :label-width="formLabelWidth">
                  <el-select v-model="form.region" placeholder="请选择活动区域">
                    <el-option label="区域一" value="shanghai"></el-option>
                    <el-option label="区域二" value="beijing"></el-option>
                  </el-select>
                </el-form-item>
                <el-form-item label="规格" :label-width="formLabelWidth">
                  <el-input v-model="form.name"></el-input>
                </el-form-item>
                <el-form-item label="商品图片" :label-width="formLabelWidth">
                  <el-input v-model="form.name"></el-input>
                </el-form-item>
              </el-form>
              <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="dialogFormVisible = false"
                  >确 定</el-button
                >
              </div>
            </el-dialog>
      <el-button class="but" type="primary">导入数据</el-button>
      <el-table highlight-current-row :data="tableData" style="width: 100%">
        <el-table-column
          type="index"
          :index="indexMethod"
          label="序号"
          width="80px"
        ></el-table-column>
        <el-table-column
          prop="skuName"
          label="商品名称"
          width="225px"
        ></el-table-column>
        <el-table-column prop="skuImage" label="商品图片" width="225px">
          <template slot-scope="scope">
            <img height="20px" :src="scope.row.skuImage" alt="" />
          </template>
        </el-table-column>
        <el-table-column prop="brandName" label="品牌" width="225px"></el-table-column>
        <el-table-column prop="unit" label="规格" width="225px"></el-table-column>
        <el-table-column prop="price" label="商品价格" width="225px"></el-table-column>
        <el-table-column prop="" label="商品类型" width="225px">饮料</el-table-column>
        <el-table-column prop="updateTime" label="创建日期" width="165px"></el-table-column>
        <el-table-column prop="address" label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              @click="handleEdit(scope.$index, scope.row)"
              >修改</el-button
            >
            <el-dialog title="收货地址" :visible.sync="dialogFormVisible">
               <el-form :model="form">
                <el-form-item label="商品名称" :label-width="formLabelWidth">
                  <el-input v-model="form.name"></el-input>
                </el-form-item>
                <el-form-item label="品牌" :label-width="formLabelWidth">
                  <el-input v-model="form.name"></el-input>
                </el-form-item>
                <el-form-item label="商品类型" :label-width="formLabelWidth">
                  <el-input v-model="form.name"></el-input>
                </el-form-item>
                <el-form-item label="商品价格" :label-width="formLabelWidth">
                  <el-select v-model="form.region" placeholder="请选择活动区域">
                    <el-option label="区域一" value="shanghai"></el-option>
                    <el-option label="区域二" value="beijing"></el-option>
                  </el-select>
                </el-form-item>
                <el-form-item label="规格" :label-width="formLabelWidth">
                  <el-input v-model="form.name"></el-input>
                </el-form-item>
                <el-form-item label="商品图片" :label-width="formLabelWidth">
                  <el-input v-model="form.name"></el-input>
                </el-form-item>
              </el-form>
              <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="dialogFormVisible = false"
                  >确 定</el-button
                >
              </div>
            </el-dialog>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="foot">
      <span>共{{ this.totalCount }}条数据&nbsp;&nbsp;第&nbsp;{{ this.currentPage }}/{{ this.totalPage }}&nbsp;页</span>
      <span class="but">
        <el-button @click="prev">上一页</el-button>
      <el-button @click="next">下一页</el-button>
      </span>
    </div>
  </div>
</template>

<script>
import { getSkuList } from '@/api/sku.js'
export default {
  data () {
    return {
      form: {},
      formLabelWidth: '100px',
      dialogFormVisible: false,
      dialogFormVisible1: false,
      tableData: [],
      pageSize: 10,
      totalCount: 0,
      totalPage: 0,
      currentPage: 1,
      input: ''
    }
  },
  async created () {
    const { currentPageRecords, totalCount, totalPage, pageSize, pageIndex } = await getSkuList()
    this.tableData = currentPageRecords
    this.totalCount = totalCount
    this.totalPage = totalPage
    this.pageSize = pageSize
    this.currentPage = pageIndex
  },
  methods: {
    async prev () {
      const data = { pageIndex: this.currentPage }
      data.pageIndex = +data.pageIndex - 1
      const { currentPageRecords, pageIndex } = await getSkuList(data)
      this.tableData = currentPageRecords
      this.currentPage = pageIndex
    },
    async next () {
      const data = { pageIndex: this.currentPage }
      data.pageIndex = +data.pageIndex + 1
      const { currentPageRecords, pageIndex } = await getSkuList(data)
      this.tableData = currentPageRecords
      this.currentPage = pageIndex
    },
    indexMethod (index) {
      return (this.currentPage - 1) * 10 + index + 1
    },
    handleEdit (index, row) {
      this.dialogFormVisible = true
    },
    handleAdd () {
      this.dialogFormVisible1 = true
    },
    async searchSku () {
      const { currentPageRecords } = await getSkuList()
      let arr = []
      const arr1 = currentPageRecords
      if (this.input) {
        arr1.forEach(item => {
          item.skuName.includes(this.input) ? arr.push(item) : arr.push()
        })
        this.tableData = arr
      } else { this.tableData = currentPageRecords }
      if (arr !== []) { arr = [] }
    }
  }
}

</script>

<style lang="scss" scoped>
.box {
  padding: 20px;
}
.search {
  margin-bottom: 50px;
  .el-input {
    display: inline-block;
    width: 200px;
    margin-right: 10px;
  }
  .el-button {
    background-color: #5f84ff;
    margin-left: 10px;
  }
}
.table {
  .newSkuDialog{
    border-radius:10px;
    box-shadow: 0 1px 3px rgb(0 0 0 / 30%);
    width: 50%;
  }
  .but {
    display: flex;
    display: inline-block;
    background: linear-gradient(135deg, #ff9743, #ff5e20);
    width: 80px !important;
    height: 36px;
    border: none;
    padding-left: 12px;
    margin-bottom: 10px;
    margin-right: 10px;
  }
}
.foot {
  margin-top: 30px;
  span {
    margin-left: 20px;
    color: #DBDFE5;
    font-size: 16px;
  }
  .but{
    margin-left: 1300px;
    .el-button {
    width: 70px;
    height: 32px;
    line-height: 32px;
    padding: 0 5px;
    border-radius: 5px;
    margin-left: 30px;
    background-color: #D5DDF8;
  }
  }

}
</style>
